package com.xianxian.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xianxian.user.entity.Department;
import com.xianxian.user.entity.Role;
import com.xianxian.user.entity.User;
import com.xianxian.user.mapper.UserMapper;
import com.xianxian.user.model.dto.UserDTO;
import com.xianxian.user.model.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 描述：
 *
 * @author zhang
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;

    public List<UserDTO> getUserList(String userName) {
        List<UserDTO> resultList = new ArrayList<>();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(null != userName && !"".equals(userName)){
            queryWrapper.lambda().like(User::getUserName,userName);
        }

        List<User> userList = baseMapper.selectList(queryWrapper);

        // 获取角色数据
        List<Role> roleList = roleService.list();
        Map<String,Role> roleMap = new HashMap<>();
        for (Role role : roleList) {
            roleMap.put(role.getId(),role);
        }

//        Map<String, Role> roleMap = roleService.list().stream().collect(Collectors.toMap(Role::getId, item -> item));
        // 获取部门数据
        Map<String, Department> departmentMap = departmentService.list().stream().collect(Collectors.toMap(Department::getId, item -> item));

        for (User user : userList) {
            UserDTO dto = new UserDTO();
            // 复制 user 属性进 dto
            BeanUtils.copyProperties(user,dto);
            Role role = roleMap.get(user.getRoleId());
            Department department = departmentMap.get(user.getDeptId());

            // 判空
            if(null == role){
                throw new RuntimeException("未找到对应角色数据。roleId=" + user.getRoleId());
            }
            if(null == department){
                throw new RuntimeException("未找到对应部门数据。deptId=" + user.getDeptId());
            }

            dto.setRoleName(role.getRoleName());
            dto.setDeptName(department.getDeptName());
            resultList.add(dto);
        }
        return resultList;
    }

    public String saveUser(UserVO userVO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserCode,userVO.getUserCode());

        User userOne = baseMapper.selectOne(queryWrapper);
        if(null != userOne){
            throw new RuntimeException("当前的用户编码已存在。userCode="+userVO.getUserCode());
        }

        User user = new User();
        BeanUtils.copyProperties(userVO,user);
        int insertNum = baseMapper.insert(user);
        if(insertNum > 0){
            return "用户信息保存成功";
        }else{
            return "用户信息保存失败";
        }
    }

    public String updateUser(User user) {
        User userOne = baseMapper.selectById(user.getId());

        if(null == userOne){
            throw new RuntimeException("根据id未找到对于用户数据");
        }

        if(!userOne.getUserCode().equals(user.getUserCode())){
            throw new RuntimeException("用户编码不可变");
        }

        int updateNum = baseMapper.updateById(user);
        if(updateNum > 0){
            return "用户修改成功";
        }else{
            return "用户修改失败";
        }
    }
}
